/**
 * This class is used to instantiate a Bishop piece that extends the piece class (abstract) with its own 
 * implementations for the movePossible method.
 * 
 * @author Dzulhafizi Abidin
 */
package pieces;
import mainClasses.*;
import java.lang.Math;


public class Bishop extends Piece{
	
	public boolean blockage = false;                 //a boolean value used to determine whether there is any piece
													  //currently blocking the bishop's movement pathway
	
	/**
	 * A constructor for the class Bishop. A Bishop piece will be instantiated with the coordinate of (xaxis,yaxis) and
	 * placed on board b, with color set to parameter color. This constructor takes 4 different parameters.
	 * 
	 * @param xaxis - The X-axis coordinate of the bishop
     * @param yaxis - The Y-axis coordinate of the bishop
     * @param b - Board where the bishop is going to be placed
     * @param color - The color of the bishop, black or white
     */
	public Bishop(int xaxis, int yaxis, Board board, String color)
	{
		super(xaxis, yaxis, board, " Bishop  ", color);				//calls the constructor in its super class, Piece
		if(color!="White")
		{
			super.type = "BK-Bishop";		//If the parameter color does not equals "White", the String type will be set to "BK-->Bishop"
											//So, if String type has no "BK", then it is a white piece
			
		}
	}
	
	/**
     * Determines whether the movement to p2 is valid for the bishop. For example, a bishop can only move diagonally but if there
     * exists any kind of piece along its pathway, it cannot proceed to move. If a piece of the opposite side is on the pathway,
     * the opposition piece can be 'eaten' by this bishop. Initially, it checks whether it's a valid move, then it checks for any 
     * interference from any other piece along the path of the movement. If all the validity tests have been passed, it returns true.
     *
     * @param p2 - Destination of the movement
     * @return A boolean value to determine whether the move is valid or not
     */
	public boolean movePossible(int desX, int desY)
	{
        int inx = this.x;
        int iny = this.y;
        if(desX>7||desX<0||desY>7||desY<0)
        {
        	return false;
        }else if (Math.abs(inx - desX) == Math.abs(iny - desY)) 
        {
            if (super.x < desX && super.y < desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = x; i < desX; i++) {
                    x++;
                    y++;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && x != super.x && x != desX) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false) 
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x > desX && super.y < desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = y; i < desY; i++) 
                {
                    y++;
                    x--;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && y != super.y && y != desY) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false)
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x > desX && super.y > desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = desY; i < y; i++) 
                {
                    y--;
                    x--;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && y != super.y && y != desY) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false) 
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            } else if (super.x < desX && super.y > desY) 
            {
                int x = super.x;
                int y = super.y;
                for (int i = x; i < desX; i++) 
                {
                    y--;
                    x++;
                    Piece temp = super.board.getPiece(x, y);
                    if (!(temp instanceof NullPiece) && x!=super.x && x!=desX) 
                    {
                        blockage = true;
                    }
                }
                if (blockage == false)
                {
                    if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
                    {
                        return true;
                    }
                }
                blockage = false;
                return false;
            }
        }
        return false;
    }
}